package User

import (
	"net/http"

	"ICan/SDK"
	M "ICan/MySql"
	"ICan/errmsg"
	"ICan/hook"
	_ "github.com/go-sql-driver/mysql"
	"github.com/gin-gonic/gin"
)
var yan2 string = "000000"
var us user
func Login1(ctx *gin.Context) {
	ver := ctx.PostForm("verification")
	token, _ := hook.SetToken(us.Name)
	if yan2 == ver {
		code :=  errmsg.Succse
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
			"data":us,
			"token":token,
		}
		ctx.JSON(http.StatusOK,data)
	}else {
		code := errmsg.Error_ver
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
		}
		ctx.JSON(http.StatusOK,data)
	}
}
func Login2(ctx *gin.Context) {
	user_phone := ctx.PostForm("user_phone")
	Mi := ctx.PostForm("password")
	sqlstr := `select user_id,user_name,user_pic,user_phone,user_password,user_email from user where user_phone = ?`
	rowObj := M.Db.QueryRow(sqlstr, user_phone)
	var u user
	rowObj.Scan(&u.User_id,&u.Name,&u.Pic,&u.User_phone,&u.Password,&u.Email)
	if u.Password == Mi {
		code :=  errmsg.Succse
		token, _ := hook.SetToken(u.Name)
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
			"data":u,
			"token":token,
		}
		ctx.JSON(http.StatusOK,data)
	}else {
		code := errmsg.Error_pass_err 
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
		}
		ctx.JSON(http.StatusOK,data)
	}
}
func F3(ctx *gin.Context) {
	user_phone := ctx.PostForm("user_phone")
	sqlstr := `select user_id,user_name,user_pic,user_phone,user_password,user_email from user where user_phone = ?`
	rowObj := M.Db.QueryRow(sqlstr, user_phone)
	rowObj.Scan(&us.User_id,&us.Name,&us.Pic,&us.User_phone,&us.Password,&us.Email)
	if us.User_id > 0 {
		yan2 = SDK.SDK3(user_phone)	
		code := errmsg.Succse
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
		}
		ctx.JSON(http.StatusOK,data)
	}else {
		code := errmsg.Error_phone_repeat
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
		}
		ctx.JSON(http.StatusOK,data)
	}
}
func Fix1(ctx *gin.Context) {
	user_email := ctx.PostForm("user_email")
	ver := ctx.PostForm("verification")
	password1 := ctx.PostForm("user_password")
	password2 := ctx.PostForm("user_password2")
	if yan2 == ver {
		if password1 == password2	{
			sqlstr := `update user set user_password=? where user_email = ?`
			ret, _ := M.Db.Exec(sqlstr,password1,user_email)
			ret.RowsAffected()
			code := errmsg.Succse
			data := map[string]interface{}{
				"code":code,
				"msg":errmsg.GetErrMSg(code),
			}
			ctx.JSON(http.StatusOK, data)
		}else {
			code := errmsg.Error_pass_not
			data := map[string]interface{}{
				"code":code,
				"msg":errmsg.GetErrMSg(code),
			}	
			ctx.JSON(http.StatusOK, data)
		}
	}else {
		code := errmsg.Error_ver
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
		}
		ctx.JSON(http.StatusOK, data)
	}
}

func F4(ctx *gin.Context) {
	user_email := ctx.PostForm("user_email")
	sqlstr := `select user_id from user where user_email = ?`
	rowObj := M.Db.QueryRow(sqlstr, user_email)
	var u user
	rowObj.Scan(&u.User_id)
	if u.User_id > 0 {
		yan2 = SDK.SDK2(user_email)
		code := errmsg.Succse
		data :=	map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
		}
		ctx.JSON(http.StatusOK, data)
	}else {
		code := errmsg.Error_email_repeat
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
		}
		ctx.JSON(http.StatusOK, data)
	}
}

func LoginQ(ctx *gin.Context){
	name := ctx.PostForm("user_name")
	pic := ctx.PostForm("user_pic")
	uid := ctx.PostForm("uid")
	sqlstr := `select user_id,user_email from user where qid = ?`
	rowObj := M.Db.QueryRow(sqlstr, uid)
	var u user
	rowObj.Scan(&u.User_id,&u.Email)
	u.Name = name
	u.Pic = pic
	if u.User_id > 0 {
		sqlstr1 := `update user set user_name=?,user_pic=? where qid = ?`
		ret, _ := M.Db.Exec(sqlstr1, name, pic, uid)
		ret.RowsAffected()
		code := errmsg.Succse
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
			"data":u,
		}
		ctx.JSON(http.StatusOK, data)
	}else {
		id := insert1(name, pic, uid)
		var us user
		us.Name = name
		us.User_id = id
		us.Pic = pic
		code := errmsg.Succse
		data := map[string]interface{}{
			"code":code,
			"msg":errmsg.GetErrMSg(code),
			"data":us,
		}
		ctx.JSON(http.StatusOK, data)
	}
}
func insert1(name, pic, uid string) int {
	var idM user
	sqlstr2 := `select user_id from user`
	rows, _ := M.Db.Query(sqlstr2)
	defer rows.Close()
	for  rows.Next() {
		if rows.Next(){
			continue
		}else {
			rows.Scan(&idM.User_id)
		}
	}
	sqlstr := `insert into user (user_id,user_name,user_phone,user_password,user_pic,qid) values (?, ?, ?, ?, ?, ?)`
	ret, _ := M.Db.Exec(sqlstr, idM.User_id, name, "", "123456", pic, uid)
	id, _ := ret.LastInsertId() //插入的id
	return int(id)
}